package bdsimple.impl.mysql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import bdsimple.abstractfactory.AbstractFactoryDAO;
import bdsimple.dao.base.ItemDAO;
import bdsimple.dao.base.TarjetaMemoriaDAO;
import bdsimple.dao.base.TarjetaMemoriaItemDAO;
import bdsimple.dao.base.TarjetaMemoriaVampiroDAO;
import bdsimple.dto.base.ItemDTO;
import bdsimple.dto.base.TarjetaMemoriaDTO;
import bdsimple.dto.base.TarjetaMemoriaItemDTO;
import bdsimple.dto.base.TarjetaMemoriaVampiroDTO;
import dao.base.impl.BaseDTO;
import dao.base.impl.Reference;

public class TarjetaMemoriaDTOImpl extends BaseDTO implements TarjetaMemoriaDTO {

	public TarjetaMemoriaDTOImpl() {
		super(TarjetaMemoriaDAO.class);
	}

	// -----------------------------------------------------------------

	private String nombre;
	
	private String descripcion;
	
	private List<TarjetaMemoriaVampiroDTO> tarjetaMemoriaVampiroDTOList;
	
	private final Reference<ItemDTO> itemRef = //
	new Reference<ItemDTO>();

	private List<TarjetaMemoriaItemDTO> tarjetaMemoriaItemDTOList;

	// -----------------------------------------------------------------

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	// -----------------------------------------------------------------

	public String getDescripcion() {
		return descripcion;
	}

	public void setDescripcion(String descripcion) {
		this.descripcion = descripcion;
	}

	// -----------------------------------------------------------------

	@SuppressWarnings("unchecked")
	public List<TarjetaMemoriaVampiroDTO> getTarjetaMemoriaVampiroDTOList() {
		if (tarjetaMemoriaVampiroDTOList == null) {
			try {

				// Lazy load the list
				TarjetaMemoriaVampiroDAO terjetaMemoriaVampiroDAO = (TarjetaMemoriaVampiroDAO) AbstractFactoryDAO
						.getFactoryDAO(). //
						getDAO(TarjetaMemoriaVampiroDAO.class, connectionBean);

				tarjetaMemoriaVampiroDTOList = new ArrayList<TarjetaMemoriaVampiroDTO>();
				tarjetaMemoriaVampiroDTOList.addAll((Collection<? extends TarjetaMemoriaVampiroDTO>) // 
						terjetaMemoriaVampiroDAO
								.listBy(TarjetaMemoriaVampiroDTOImpl.ID, id));

			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

		return tarjetaMemoriaVampiroDTOList;
	}

	public void setTarjetaMemoriaVampiroDTOList(List<TarjetaMemoriaVampiroDTO> tarjetaMemoriaVampiroDTOList) {
		throw new UnsupportedOperationException();
	}
	
	@SuppressWarnings("unchecked")
	public List<TarjetaMemoriaItemDTO> getTarjetaMemoriaItemDTOList() {
		if (tarjetaMemoriaItemDTOList == null) {
			try {

				// Lazy load the list
				TarjetaMemoriaItemDAO terjetaMemoriaItemDAO = (TarjetaMemoriaItemDAO) AbstractFactoryDAO
						.getFactoryDAO(). //
						getDAO(TarjetaMemoriaItemDAO.class, connectionBean);

				tarjetaMemoriaItemDTOList = new ArrayList<TarjetaMemoriaItemDTO>();
				tarjetaMemoriaItemDTOList.addAll((Collection<? extends TarjetaMemoriaItemDTO>) // 
						terjetaMemoriaItemDAO
								.listBy(TarjetaMemoriaItemDTOImpl.ID, id));

			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

		return tarjetaMemoriaItemDTOList;
	}

	public void setTarjetaMemoriaItemDTOList(List<TarjetaMemoriaItemDTO> tarjetaMemoriaItemDTOList) {
		throw new UnsupportedOperationException();
	} 
	
	// -----------------------------------------------------------------
	@Override
	  public ItemDTO getItemDTORef() {
	    if (itemRef.getRefValue() == null) {
	      if (itemRef.getRefIdent() != 0) {
	        try {

	          // Lazy load the itemDTORef
	          ItemDAO itemDAO = (ItemDAO) AbstractFactoryDAO.getFactoryDAO(). //
	              getDAO(ItemDAO.class, connectionBean);

	          itemRef.setRefValue( //
	              (ItemDTO) itemDAO.loadById(itemRef.getRefIdent()));

	        } catch (Exception e) {
	          e.printStackTrace();
	          throw new RuntimeException(e);
	        }
	      }
	    }

	    return itemRef.getRefValue();
	  }

	  @Override
	  public void setItemDTORef(ItemDTO itemDTORef) {
	    this.itemRef.setRefValue(itemDTORef);
	  }

	  // --------------------------------------------------------------------------------

	  public Reference<ItemDTO> getItemRef() {
	    return itemRef;
	  }
	
	// -----------------------------------------------------------------


}
